package top.hidoctor.auscultation.feature.fragment.work;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tang.user.UserInfo;
import com.tang.user.UserInfoManager;
import com.tang.util.DensityUtil;

import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.WorkAdapter;
import top.hidoctor.auscultation.base.BaseFragment;
import top.hidoctor.auscultation.bean.NewsBean;
import top.hidoctor.auscultation.feature.activity.BaizhubaoListActivity;
import top.hidoctor.auscultation.feature.activity.ExampleChooseActivity;
import top.hidoctor.auscultation.feature.activity.GongzuotaiShActivity;
import top.hidoctor.auscultation.feature.activity.MyXyListActivity;
import top.hidoctor.auscultation.feature.activity.NewsActivity;
import top.hidoctor.auscultation.feature.activity.ShenhebaoActivity;
import top.hidoctor.auscultation.feature.activity.GongzuotaiBzActivity;
import top.hidoctor.auscultation.feature.activity.ZhongcaiListActivity;
import top.hidoctor.auscultation.net.c.NewsContract;
import top.hidoctor.auscultation.net.p.NewsListPresenter;
import top.hidoctor.auscultation.net.req.PageReq;

public class WorkFragment extends BaseFragment implements View.OnClickListener, NewsContract.View {

    private ImageButton ibNews;
    private Intent intent;
    private RecyclerView rcWork;
    private WorkAdapter adapter;
    private NewsListPresenter presenter;
    private TextView tvPoint;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_work, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter = new NewsListPresenter(getContext(), this);
        presenter.getList(null);
        initView();
    }

    private void initView() {
        List<UserInfo.MenuListBean> menuList = UserInfoManager.getInstance().getUserInfo().getMenuList();
        ibNews = getActivity().findViewById(R.id.ib_news);
        rcWork = getActivity().findViewById(R.id.rc_work);
        tvPoint = getActivity().findViewById(R.id.tv_point);

        rcWork.setLayoutManager(new GridLayoutManager(getContext(), 2));
        rcWork.setAdapter(adapter = new WorkAdapter(R.layout.item_work, menuList));
        rcWork.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int space = DensityUtil.dip2px(getContext(), 20);
                int colums = parent.getChildAdapterPosition(view) % 2;
                outRect.left = colums * space / 2;
                outRect.right = space - (colums + 1) * space / 2;
            }
        });

        adapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                UserInfo.MenuListBean item = adapter.getData().get(position);
                if (item.getTitle().equals("我的心音样本")) {
                    startActivity(new Intent(getContext(), MyXyListActivity.class));
                } else if (item.getTitle().equals("标注包")) {
                    startActivity(new Intent(getContext(), BaizhubaoListActivity.class));
                } else if (item.getTitle().equals("标注工作台")) {
                    intent = new Intent(getContext(), GongzuotaiBzActivity.class);
                    startActivity(intent);
                } else if (item.getTitle().equals("审核包")) {
                    startActivity(new Intent(getContext(), ShenhebaoActivity.class));
                } else if (item.getTitle().equals("审核工作台")) {
                    intent = new Intent(getContext(), GongzuotaiShActivity.class);
                    startActivity(intent);
                } else if (item.getTitle().equals("样本仲裁")) {
                    startActivity(new Intent(getContext(), ZhongcaiListActivity.class));
                }
            }
        });
        ibNews.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_news:
                startActivity(new Intent(getContext(), NewsActivity.class));
                break;

        }
    }

    @Override
    public void getListSuc(List<NewsBean> bean) {
        if (bean.size() == 0) {
            tvPoint.setVisibility(View.GONE);
        } else {
            tvPoint.setVisibility(View.VISIBLE);
            tvPoint.setText(bean.size() + "");
        }
    }
}
